package com.mall.config;

import org.quartz.Scheduler;
import org.quartz.ee.servlet.QuartzInitializerListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import java.io.IOException;
import java.util.Properties;

@Configuration
public class QuartzSchedulerConfig {

    @Autowired
    private MyJobFactory jobFactory;

    @Bean
    public Properties getProperties() throws IOException{
        PropertiesFactoryBean bean = new PropertiesFactoryBean();
        //加载属性文件
        bean.setLocation(new ClassPathResource("/quartz.properties"));
        bean.afterPropertiesSet();
        return bean.getObject();
    }

    @Bean
    public SchedulerFactoryBean factoryBean() throws IOException{
        SchedulerFactoryBean factory = new SchedulerFactoryBean();
        factory.setQuartzProperties(getProperties());
        factory.setJobFactory(jobFactory);
        return factory;
    }

    @Bean
    public Scheduler scheduler(){
        try{
            return factoryBean().getScheduler();
        }catch (IOException e){
            e.printStackTrace();
        }
        return null;
    }

    @Bean
    public QuartzInitializerListener listener(){
        return new QuartzInitializerListener();
    }


}
